//
//  ZAppraiseViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/22.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZAppraiseViewController.h"
#import "LZComDoctorVC.h"
static int startTag  = 800;
@interface ZAppraiseViewController (){
    NSArray <UIButton *>* _buttonAry;
    UILabel *  _titleLabel;
    UIButton * _startBtn1;// 5个button;
    UIButton * _startBtn2;
    UIButton * _startBtn3;
    UIButton * _startBtn4;
    UIButton * _startBtn5;
    UITextView * _appraiseTextView;
    UILabel * _placeholder;
    UIButton * _complainBtn;// 投诉按钮
    UIButton * _appraiseBtn;// 评价按钮
}
@property (nonatomic,assign) NSInteger  scoce;//分数
@end

@implementation ZAppraiseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initialzeDatas];
    [self initlizeInterface];
}

- (void)initialzeDatas {
    _scoce = 5;
}

- (void)initlizeInterface {
    self.navigationItem.title = @"评价";
    self.view.backgroundColor = [UIColor whiteColor];
    [self makeAllSubView];
    [self addAllSubView];
    [self makeAllSubViewMasnoy];
}

- (void)makeAllSubView {
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    _titleLabel.text = @"请你对本次服务进行评价";
    _startBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    _startBtn1.tag = startTag +1 ;
    _startBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _startBtn2.tag = startTag +2 ;
    _startBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    _startBtn3.tag = startTag +3 ;
    _startBtn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    _startBtn4.tag = startTag +4 ;
    _startBtn5 = [UIButton buttonWithType:UIButtonTypeCustom];
    _startBtn5.tag = startTag +5 ;
    [_startBtn1 addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
    [_startBtn2 addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
    [_startBtn3 addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
    [_startBtn4 addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
    [_startBtn5 addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
    _buttonAry = @[_startBtn1,_startBtn2,_startBtn3,_startBtn4,_startBtn5];
    [self setAllStartBtnWith:YES];//设置小星星
    _appraiseTextView = [[UITextView alloc] init];
    _appraiseTextView.layer.borderColor = [MYGRAYCOLOR CGColor] ;
    _appraiseTextView.layer.cornerRadius = 4;
    _appraiseTextView.layer.borderWidth = 2;
    _appraiseTextView.layer.masksToBounds = YES;
    _placeholder = [[UILabel alloc] init];
    _complainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_complainBtn setTitle:@"非常不满意，我要投诉" forState:UIControlStateNormal];
    [_complainBtn addTarget:self action:@selector(jumpComplainVC) forControlEvents:UIControlEventTouchUpInside];
    _complainBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_complainBtn setTitleColor:MYGREENColor forState:UIControlStateNormal];
    _appraiseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_appraiseBtn setBackgroundColor:MYGREENColor];
    [_appraiseBtn setTitle:@"立即评价" forState:UIControlStateNormal];
    [_appraiseBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_appraiseBtn addTarget:self action:@selector(appraiseNetWorking) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setAllStartBtnWith:(BOOL)type {
    if (type) {
        for (UIButton * button in _buttonAry) {
            [button setBackgroundImage:[UIImage imageNamed:@"comment_icon_star_1"] forState:UIControlStateNormal];
        }
    }else {
        for (UIButton * button in _buttonAry) {
            [button setBackgroundImage:[UIImage imageNamed:@"comment_icon_star_2"] forState:UIControlStateNormal];
        }

    }
    
}
- (void)addAllSubView {
    [self.view addSubview:_titleLabel];
    [self.view addSubview:_startBtn1];
    [self.view addSubview:_startBtn2];
    [self.view addSubview:_startBtn3];
    [self.view addSubview:_startBtn4];
    [self.view addSubview:_startBtn5];
    [self.view addSubview:_appraiseTextView];
    [self.view addSubview:_placeholder];
    [self.view addSubview:_complainBtn];
    [self.view addSubview:_appraiseBtn];
}

- (void)makeAllSubViewMasnoy {
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15*xsIZE);
        make.width.offset(200*xsIZE);
        make.height.offset(15*ysIZE);
        make.top.equalTo(self.view).offset(37*ysIZE);
    }];
    [_startBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel);
        make.width.height.offset(30*xsIZE);
        make.top.equalTo(_titleLabel.mas_bottom).offset(15*ysIZE);
    }];
    [_startBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_startBtn1);
        make.left.equalTo(_startBtn1.mas_right).offset(10*xsIZE);
    }];
    [_startBtn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_startBtn2);
        make.left.equalTo(_startBtn2.mas_right).offset(10*xsIZE);
    }];
    [_startBtn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_startBtn3);
        make.left.equalTo(_startBtn3.mas_right).offset(10*xsIZE);
    }];
    [_startBtn5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_startBtn4);
        make.left.equalTo(_startBtn4.mas_right).offset(10*xsIZE);
    }];
    [_appraiseTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15*xsIZE);
        make.right.equalTo(self.view).offset(-15*xsIZE);
        make.height.offset(240*xsIZE);
        make.top.equalTo(_startBtn1.mas_bottom).offset(20*ysIZE);
    }];
    [_complainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_appraiseTextView);
        make.height.offset(15*ysIZE);
        make.width.offset(180*xsIZE);
        make.top.equalTo(_appraiseTextView.mas_bottom).offset(15*ysIZE);
    }];
    [_appraiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_appraiseTextView);
        make.width.equalTo(_appraiseTextView);
        make.height.offset(45*ysIZE);
        make.top.equalTo(_complainBtn.mas_bottom).mas_offset(35*ysIZE);
    }];
}

- (void)startAction:(UIButton *)sender {
    NSInteger  index = sender.tag - startTag ;
    [self setAllStartBtnWith:NO];
    for (int i = 0; i < index; i++) {
        [_buttonAry[i] setBackgroundImage:[UIImage imageNamed:@"comment_icon_star_1"] forState:UIControlStateNormal];
    }
    _scoce = index ;
    
}

- (void)appraiseNetWorking {
    _appraiseBtn.userInteractionEnabled = NO;
    if (!_appraiseTextView.text || [_appraiseTextView.text isEqualToString:@""]) {
        [YJProgressHUD showMessage:@"评价内容不能为空" inView:self.view];
        return;
    }
    if (!self.order_num || [self.order_num isEqualToString:@""] ) {
        [YJProgressHUD showMessage:@"订单号有误" inView:self.view];
        return;
    }
    NSString * content = [_appraiseTextView.text stringByReplacingOccurrencesOfString:@"\"" withString:@"”" ];
    [YJProgressHUD showProgress:@"评价中" inView:self.view];
    NSDictionary * dic = @{@"order_num":self.order_num,@"score":[NSString stringWithFormat:@"%ld",(long)_scoce],@"content":content};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dic paramarsSite:@"Order/evaluate" sucessBlock:^(id content) {
        _appraiseBtn.userInteractionEnabled = YES;
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"评价成功" inView:self.view];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else {
            [YJProgressHUD showMessage:@"评价失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
    }];
}
- (void)jumpComplainVC {
    LZComDoctorVC * vc = [[LZComDoctorVC alloc] init];
    vc.order_num = self.order_num;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
